package org.dromara.demo.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.dromara.demo.domain.JsCaseVar;
import org.dromara.demo.domain.vo.JsExampleVo;
import org.dromara.demo.mapper.JsCaseVarMapper;
import org.springframework.stereotype.Service;
import org.dromara.demo.domain.bo.JsSuiteBasicVarBo;
import org.dromara.demo.domain.vo.JsSuiteBasicVarVo;
import org.dromara.demo.domain.JsSuiteBasicVar;
import org.dromara.demo.mapper.JsSuiteBasicVarMapper;
import org.dromara.demo.service.IJsSuiteBasicVarService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 套件参数Service业务层处理
 *
 * @author zhu
 * @date 2025-02-18
 */
@RequiredArgsConstructor
@Service
public class JsSuiteBasicVarServiceImpl implements IJsSuiteBasicVarService {

    private final JsSuiteBasicVarMapper baseMapper;
    private final JsCaseVarMapper jsCaseVarMapper;

    /**
     * 查询套件参数
     *
     * @param id 主键
     * @return 套件参数
     */
    @Override
    public JsSuiteBasicVarVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询套件参数列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 套件参数分页列表
     */
    @Override
    public TableDataInfo<JsSuiteBasicVarVo> queryPageList(JsSuiteBasicVarBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<JsSuiteBasicVar> lqw = buildQueryWrapper(bo);
        Page<JsSuiteBasicVarVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的套件参数列表
     *
     * @param bo 查询条件
     * @return 套件参数列表
     */
    @Override
    public List<JsSuiteBasicVarVo> queryList(JsSuiteBasicVarBo bo) {
        LambdaQueryWrapper<JsSuiteBasicVar> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<JsSuiteBasicVar> buildQueryWrapper(JsSuiteBasicVarBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<JsSuiteBasicVar> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getSuiteId() != null, JsSuiteBasicVar::getSuiteId, bo.getSuiteId());
        lqw.eq(StringUtils.isNotBlank(bo.getMachineType()), JsSuiteBasicVar::getMachineType, bo.getMachineType());
        lqw.eq(StringUtils.isNotBlank(bo.getCom()), JsSuiteBasicVar::getCom, bo.getCom());
        lqw.eq(StringUtils.isNotBlank(bo.getProductId()), JsSuiteBasicVar::getProductId, bo.getProductId());
        lqw.eq(StringUtils.isNotBlank(bo.getClientId()), JsSuiteBasicVar::getClientId, bo.getClientId());
        lqw.like(StringUtils.isNotBlank(bo.getPlatformName()), JsSuiteBasicVar::getPlatformName, bo.getPlatformName());
        lqw.eq(StringUtils.isNotBlank(bo.getPlatformVersion()), JsSuiteBasicVar::getPlatformVersion, bo.getPlatformVersion());
        lqw.like(StringUtils.isNotBlank(bo.getDeviceName()), JsSuiteBasicVar::getDeviceName, bo.getDeviceName());
        lqw.eq(StringUtils.isNotBlank(bo.getServerUrl()), JsSuiteBasicVar::getServerUrl, bo.getServerUrl());
        return lqw;
    }

    /**
     * 新增套件参数
     *
     * @param bo 套件参数
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(JsSuiteBasicVarBo bo) {
        JsSuiteBasicVar add = MapstructUtils.convert(bo, JsSuiteBasicVar.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改套件参数
     *
     * @param bo 套件参数
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(JsSuiteBasicVarBo bo) {
        List<JsCaseVar> jsCaseVars = new ArrayList<>();
        List<JsExampleVo> jsExampleVos = bo.getJsExampleVos();
        for (JsExampleVo jsExampleVo : jsExampleVos) {
            jsCaseVars.addAll(BeanUtil.copyToList(jsExampleVo.getJsCaseVarVos(), JsCaseVar.class));
        }
        JsSuiteBasicVar update = MapstructUtils.convert(bo, JsSuiteBasicVar.class);
        validEntityBeforeSave(update);
        boolean b = true;
        if (jsCaseVars.size() > 0){
            b = jsCaseVarMapper.updateBatchById(jsCaseVars);
        }
        return baseMapper.updateById(update) > 0 && b;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(JsSuiteBasicVar entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除套件参数信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }

    @Override
    public JsSuiteBasicVarVo queryBySuiteId(Long suiteId) {
        JsSuiteBasicVarVo jsSuiteBasicVarVo = baseMapper.selectVoOne(Wrappers.lambdaQuery(JsSuiteBasicVar.class).eq(JsSuiteBasicVar::getSuiteId, suiteId));
        return jsSuiteBasicVarVo;
    }

    @Override
    public int updateBySuiteId(Long suiteId, String exampleId) {

        return baseMapper.update(new LambdaUpdateWrapper<JsSuiteBasicVar>().set(JsSuiteBasicVar::getExampleIds, exampleId).eq(JsSuiteBasicVar::getSuiteId, suiteId));
    }
}
